package com.tyk.demo.controller;

import com.github.pagehelper.PageInfo;
import com.tyk.demo.dto.UserInfo;
import com.tyk.demo.service.UserInfoService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Api(description = "用户信息")
@RestController
public class UserInfoController {
    private static final String PREV = "/api/v1/user";
    private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoController.class);
    @Resource
    private UserInfoService userinfoService;

    /**
     * [新增]
     * @author 大狼狗
     * @date 2020/07/01
     **/
    @ApiOperation(value = "创建用户",notes = "根据User对象创建用户")
//    @ApiImplicitParam(required = true,name = "userinfo",value = "用户详细实体userinfo")
    @PostMapping("/insert/")
    public boolean insert(@RequestBody UserInfo userinfo){
        return userinfoService.insert(userinfo);
    }

    /**
     * [刪除]
     * @author 大狼狗
     * @date 2020/07/01
     **/
    @ApiOperation(value = "删除用户",notes = "根据用户id删除用户")
    @DeleteMapping("/delete/{id}")
    public boolean delete(@ApiParam(value = "用户id",required = true)@PathVariable(value = "id") String id){
        return userinfoService.delete(id);
    }

    /**
     * [更新]
     * @author 大狼狗
     * @date 2020/07/01
     **/
    @ApiOperation(value = "更新用户",notes = "根据用户信息更新用户")
    @PutMapping("/update/")
    public boolean update(@ApiParam(value = "userInfo",required = true)@RequestBody UserInfo userInfo){
        return userinfoService.update(userInfo);
    }

    /**
     * [查询] 根据主键 id 查询
     * @author 大狼狗
     * @date 2020/07/01
     **/
    @ApiOperation(value = "查询用户",notes = "根据用户id查询用户")
    @GetMapping("/get/{id}")
    public UserInfo findUserById(@ApiParam(value = "id",required = true) @PathVariable(value = "id") String id){
        LOGGER.info("开始查询id为{}的用户",id);
        return userinfoService.load(id);
    }

    /**
     * [查询] 分页查询
     * @author 大狼狗
     * @date 2020/07/01
     **/
    @ApiOperation(value = "分页查询",notes = "分页查询")
    @GetMapping("/pageList")
    public Map<String, Object> pageList(@ApiParam(value = "offset")@RequestParam(required = false, defaultValue = "0") int offset,
                                        @ApiParam(value = "pageSize")@RequestParam(required = false, defaultValue = "10") int pageSize,
                                        UserInfo userInfo) {
        return userinfoService.pageList(offset, pageSize);
    }

    public ResponseEntity<UserInfo> getById(String userId) {
        return new ResponseEntity<>(userinfoService.load(userId), HttpStatus.OK);
    }

    /**
     * [查询] 分页查询
     * @author 大狼狗
     * @date 2020/07/01
     **/
    @ApiOperation(value = "分页查询PageHelper",notes = "分页查询PageHelper")
    @GetMapping("/pageList2")
    public PageInfo<UserInfo> pageList2(UserInfo userInfo) {
        return new PageInfo<>(userinfoService.pageList2(userInfo));
    }
}
